package com.fas.hehe.system.service;

import java.util.ArrayList;
import java.util.Map;

import org.junit.Assert;

import com.fas.core.BaseService;
import com.fas.core.plugin.datatables.PageRequest;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 
 * 登录日志业务处理类
 *
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午8:05:55
 * @version V1.0
 */
public class LoginLogService extends BaseService {

	/**
	 * 
	 * @Title:分页查询
	 * @Description: 
	 * @param request
	 * 				分页查询请求对象(For datatables插件)
	 * @return     
	 * @anthor yixiaoping<thinker163@163.com>
	 * @date 2016年1月23日 下午8:06:34
	 */
	public Page<Record> queryPage(PageRequest request) {
		Assert.assertNotNull("请求参数不能为空", request);

		Map<String, Object> paramsMap = request.getParams();
		ArrayList<Object> params = new ArrayList<Object>();
		String selectSql = "select * ";
		StringBuffer whereSql = new StringBuffer();
		whereSql.append(" from sys_login_log where 1=1 ");

		if (StrKit.notBlank((String) paramsMap.get("account"))) {
			whereSql.append(" and account = ?");
			params.add(paramsMap.get("account"));
		}
		if (StrKit.notBlank((String) paramsMap.get("begin_logintime"))) {
			whereSql.append(" and login_time >= ?");
			params.add(paramsMap.get("begin_logintime"));
		}

		if (StrKit.notBlank((String) paramsMap.get("end_logintime"))) {
			whereSql.append(" and login_time <=  ?");
			params.add(paramsMap.get("end_logintime"));
		}

		if (StrKit.notBlank((String) request.getOderby())) {
			whereSql.append(" order by ").append(request.getOderby());
		}

		return Db.paginate(request.getPageNumber(), request.getPageSize(), selectSql, whereSql.toString(),
				params.toArray());
	}

}
